package org.jeecg.modules.demo.dajy.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.TreeBuilder;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.api.service.IApiInfoService;
import org.jeecg.modules.demo.dajy.entity.DaJyxx;
import org.jeecg.modules.demo.dajy.service.IDaJyxxService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo.dwxx.entity.SysDwxx;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 复印调档表
 * @Author: jeecg-boot
 * @Date:   2023-11-27
 * @Version: V1.0
 */
@Api(tags="复印调档表")
@RestController
@RequestMapping("/dajy/daJyxx")
@Slf4j
public class DaJyxxController extends JeecgController<DaJyxx, IDaJyxxService> {
	 @Autowired
	 private IDaJyxxService daJyxxService;
	 @Autowired
	 public ISysBaseAPI sysBaseAPI;
	 @Autowired
	 private IApiInfoService apiInfoService;
	/**
	 * 分页列表查询
	 *
	 * @param daJyxx
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "复印调档表-分页列表查询")
	@ApiOperation(value="复印调档表-分页列表查询", notes="复印调档表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<DaJyxx>> queryPageList(DaJyxx daJyxx,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<DaJyxx> queryWrapper = QueryGenerator.initQueryWrapper(daJyxx, req.getParameterMap());
		Page<DaJyxx> page = new Page<DaJyxx>(pageNo, pageSize);
		String jyr = req.getParameter("jyr");
		String czlx = req.getParameter("czlx");
		IPage<DaJyxx> pageList = daJyxxService.getlist(page,jyr, czlx);
		return Result.OK(pageList);
	}


	 @ApiOperation(value="复印调档表-用户列表", notes="复印调档表-用户列表")
	 @GetMapping(value = "/dauserlist")
	 public String getuserlist(HttpServletRequest request, HttpServletResponse response) {
		 LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String sysOrgCode  = loginUser.getOrgCode();
		 String result = daJyxxService.dauserlist().toString();
		 return result;
	 }

	 @ApiOperation(value="回传文件列表-回传文件列表", notes="回传文件列表-回传文件列表")
	 @GetMapping(value = "/hcwjlist")
	 public String hcwjlist(HttpServletRequest request, HttpServletResponse response) {
		 LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String sysOrgCode  = loginUser.getOrgCode();
		 String id = request.getParameter("id");
		 String result = daJyxxService.gethcwjlist(id).toString();
		 return result;
	 }
	 /**
	  * 分页列表查询
	  *
	  * @param daJyxx
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 //@AutoLog(value = "复印调档表-分页列表查询")
	 @ApiOperation(value="复印调档表-分页列表查询", notes="复印调档表-分页列表查询")
	 @GetMapping(value = "/fllist")
	 public Result<IPage<DaJyxx>> queryPageflList(DaJyxx daJyxx,
												@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												HttpServletRequest req) {
		 QueryWrapper<DaJyxx> queryWrapper = QueryGenerator.initQueryWrapper(daJyxx, req.getParameterMap());
		 LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		 String sysOrgCode  = loginUser.getOrgCode();
		 Page<DaJyxx> page = new Page<DaJyxx>(pageNo, pageSize);
		 String jyr = req.getParameter("jyr");
		 String czlx = req.getParameter("czlx");
		 IPage<DaJyxx> pageList = daJyxxService.getxxByUser(page,loginUser.getUsername(),loginUser.getId(),jyr,czlx);
		 return Result.OK(pageList);
	 }
	 /**
	  *   借阅详情目录
	  *
	  * @return
	  */
	 @AutoLog(value = "借阅详情目录")
	 @ApiOperation(value="借阅详情目录", notes="借阅详情目录")
	 @PostMapping(value = "/jymllist")
	 public String jymllist(HttpServletRequest request, HttpServletResponse response) {
		 LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String sysOrgCode  = loginUser.getOrgCode();
		 String id = request.getParameter("id");
		 List<TreeBuilder.Node> nodeList = new ArrayList<>();
//		 String result = new TreeBuilder().buildTree(daJyxxService.jymllist(id));
		 String result = daJyxxService.jymllist(id).toString();
		 return result;
	 }

	 /**
	  *   借阅审核
	  *
	  * @return
	  */
	 @AutoLog(value = "借阅审核")
	 @ApiOperation(value="借阅审核", notes="借阅审核")
	 @PostMapping(value = "/jyshenhe")
	 public String jyshenhe(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
		 LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String sysOrgCode  = loginUser.getOrgCode();
		 String id = jsonObject.get("id").toString();
		 String createby = jsonObject.get("createby").toString();

		 String jyr = ""; if (jsonObject.get("jyr") != null){	jyr =	 jsonObject.get("jyr").toString(); }
		 String bjyrid = ""; if (jsonObject.get("bjyrid") != null) { bjyrid = jsonObject.get("bjyrid").toString();}
		 String zt = ""; if (jsonObject.get("zt") != null ) { zt = jsonObject.get("zt").toString();}
		 String bhyy = ""; if (jsonObject.get("bhyy") != null) {bhyy = jsonObject.get("bhyy").toString();}
		 if (zt.equals("21")){
			 JSONObject temp = apiInfoService.getqxuser("1730137865066786818");
			 for (int i = 0; i < temp.getJSONArray("data").size(); i++){
				 JSONObject object = temp.getJSONArray("data").getJSONObject(i);
				 String username = object.getString("username");
				 MessageDTO message = new MessageDTO(loginUser.getUsername(), username, jyr+"调用档案初审核通过", jyr+"申请调用档案初审核通过", "1");
				 // 是否发送给全部人
				 message.setToAll(false);
				 sysBaseAPI.sendSysAnnouncement(message);
			 }
		 }
		 if (zt.equals("20")){
			 MessageDTO message = new MessageDTO(loginUser.getUsername(), createby, jyr+"调用档案初审核不通过", bhyy, "1");
			 // 是否发送给全部人
			 message.setToAll(false);
			 sysBaseAPI.sendSysAnnouncement(message);
		 }
		 if (zt.equals("31")){
			 MessageDTO message = new MessageDTO(loginUser.getUsername(), createby, jyr+"调用档案审核通过", jyr+"调用档案审核通过", "1");
			 // 是否发送给全部人
			 message.setToAll(false);
			 sysBaseAPI.sendSysAnnouncement(message);
		 }
		 if (zt.equals("30")){
			 MessageDTO message = new MessageDTO(loginUser.getUsername(), createby, jyr+"调用档案复审核不通过", bhyy, "1");
			 // 是否发送给全部人
			 message.setToAll(false);
			 sysBaseAPI.sendSysAnnouncement(message);
		 }
		 if (zt.equals("11")){
			 MessageDTO message = new MessageDTO(loginUser.getUsername(), createby, jyr+"复印档案审核通过", jyr+"复印档案审核通过", "1");
			 // 是否发送给全部人
			 message.setToAll(false);
			 sysBaseAPI.sendSysAnnouncement(message);
		 }

		 String result = daJyxxService.jyshenhe(loginUser.getUsername(),sysOrgCode,id,zt,loginUser.getRealname(),bjyrid,bhyy).toString();
		 return result;
	 }

	 /**
	  *   添加
	  *
	  * @return
	  */
	 @AutoLog(value = "目录文件表-添加")
	 @ApiOperation(value="目录文件表-添加", notes="目录文件表-添加")
	 @PostMapping(value = "/add")
	 public String add(HttpServletRequest request,@RequestBody JSONObject jsonObject) {
		 LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String sysOrgCode  = loginUser.getOrgCode();
		 String jyr = jsonObject.get("jyr").toString();
		 String lx = jsonObject.get("lx").toString();
		 String bjyrxm = jsonObject.get("bjyrxm").toString();
		 String bjyrsfzh = jsonObject.get("bjyrsfzh").toString();
		 String bjyrrwsj = jsonObject.get("bjyrrwsj").toString();
		 String bjyrtwsj = jsonObject.get("bjyrtwsj").toString();
		 String bjyrlxdh = jsonObject.get("bjyrlxdh").toString();
		 String bjyrid = jsonObject.get("bjyrid").toString();
		 String sqyy = jsonObject.get("sqyy").toString();
		 String czlx = jsonObject.get("czlx").toString();
		 String dwmc = ""; if (jsonObject.get("dwmc") != null) { dwmc = jsonObject.get("dwmc").toString(); }
		 JSONArray filelist1 =  jsonObject.getJSONArray("filelist1");
		 JSONArray filelist2 =  jsonObject.getJSONArray("filelist2");
		 JSONArray filelist3 =  jsonObject.getJSONArray("filelist3");
		 JSONArray filelist4 =  jsonObject.getJSONArray("filelist4");
		 JSONArray filelist5 =  jsonObject.getJSONArray("filelist5");
		 JSONArray filelist6 =  jsonObject.getJSONArray("filelist6");
		 JSONArray daarray =  jsonObject.getJSONArray("xzdawjlist");
		 String qmdata = ""; if (jsonObject.get("qmdata") != null) { qmdata = jsonObject.get("qmdata").toString(); }
         String cz = "";
         JSONObject temp = null;
		 if (czlx == "1"){
		 	cz = "复印档案";
			temp = apiInfoService.getqxuser("1730137636376555521");
		 }else{
		 	 cz="调用档案";
			 temp = apiInfoService.getqxuser("1730137636376555521");
		 }

		 for (int i = 0; i < temp.getJSONArray("data").size(); i++){
			 JSONObject object = temp.getJSONArray("data").getJSONObject(i);
			 String username = object.getString("username");
			 MessageDTO message = new MessageDTO(loginUser.getUsername(), username, jyr+"申请"+cz, jyr+"申请"+cz+" 申请原因："+sqyy, "1");
			 // 是否发送给全部人
			 message.setToAll(false);
			 sysBaseAPI.sendSysAnnouncement(message);
		 }
		 String result = daJyxxService.addmlwj(loginUser.getUsername(),sysOrgCode,jyr,czlx,filelist1,filelist2,filelist3,filelist4,filelist5,filelist6,daarray,bjyrxm,bjyrid,sqyy,bjyrsfzh,
				 bjyrrwsj,bjyrtwsj,bjyrlxdh,dwmc,qmdata).toString();
		 return result;
	 }

	 /**
	  *   添加
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "回传目录文件-添加")
	 @ApiOperation(value="回传目录文件-添加", notes="回传目录文件-添加")
	 @PostMapping(value = "/hcadd")
	 public String hcadd(HttpServletRequest request,@RequestBody JSONObject jsonObject) {
		 LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String sysOrgCode  = loginUser.getOrgCode();
		 String id = jsonObject.get("id").toString();
		 String lx = jsonObject.get("lx").toString();
		 JSONArray wjarray =  jsonObject.getJSONArray("filelist");

		 String result = daJyxxService.addhcwj(loginUser.getUsername(),sysOrgCode,id,wjarray,lx).toString();
		 return result;
	 }
//
//	/**
//	 *   添加
//	 *
//	 * @param daJyxx
//	 * @return
//	 */
//	@AutoLog(value = "复印调档表-添加")
//	@ApiOperation(value="复印调档表-添加", notes="复印调档表-添加")
//	@PostMapping(value = "/add")
//	public Result<String> add(@RequestBody DaJyxx daJyxx) {
//		daJyxxService.save(daJyxx);
//		return Result.OK("添加成功！");
//	}
	
	/**
	 *  编辑
	 *
	 * @param daJyxx
	 * @return
	 */
	@AutoLog(value = "复印调档表-编辑")
	@ApiOperation(value="复印调档表-编辑", notes="复印调档表-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody DaJyxx daJyxx) {
		daJyxxService.updateById(daJyxx);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "复印调档表-通过id删除")
	@ApiOperation(value="复印调档表-通过id删除", notes="复印调档表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		daJyxxService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "复印调档表-批量删除")
	@ApiOperation(value="复印调档表-批量删除", notes="复印调档表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.daJyxxService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "复印调档表-通过id查询")
	@ApiOperation(value="复印调档表-通过id查询", notes="复印调档表-通过id查询")
	@GetMapping(value = "/queryById")
	public String  queryById(@RequestParam(name="id",required=true) String id) {
		JSONObject temp = daJyxxService.getJyxx(id);
		return temp.toJSONString();
	}

    /**
    * 导出excel
    *
    * @param request
    * @param daJyxx
    */
    @RequiresPermissions("dajy:da_jyxx:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, DaJyxx daJyxx) {
        return super.exportXls(request, daJyxx, DaJyxx.class, "复印调档表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("dajy:da_jyxx:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, DaJyxx.class);
    }

}
